package cn.sc.summer.sms.service.impl;

import cn.sc.summer.sms.service.SmsService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 类名：发送短信实体类
 *
 * @author a-xin
 * @date 2023/12/15 15:04
 */
@Slf4j
@Service
public class TencentSmsImpl implements SmsService {

    @Override
    public boolean sendSms(List<String> params, Map<String, String> configParams, String templateId, String... phone) {
        try {

            //这里是实例化一个Credential，也就是认证对象，参数是密钥对；你要使用肯定要进行认证
            Credential credential = new Credential(configParams.get("secretId"), configParams.get("secretKey"));
            //HttpProfile这是http的配置文件操作，比如设置请求类型(post,get)或者设置超时时间了、还有指定域名了
            //最简单的就是实例化该对象即可，它的构造方法已经帮我们设置了一些默认的值
            HttpProfile httpProfile = new HttpProfile();

            //实例化一个客户端配置对象,这个配置可以进行签名（使用私钥进行加密的过程），对方可以利用公钥进行解密
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            //实例化要请求产品(以sms为例)的client对象
            SmsClient smsClient = new SmsClient(credential, "ap-beijing", clientProfile);

            //实例化request封装请求信息
            SendSmsRequest request = new SendSmsRequest();
            request.setPhoneNumberSet(phone);
            request.setSmsSdkAppid(configParams.get("appId"));
            request.setSign(configParams.get("signName"));
            request.setTemplateID(templateId);
            request.setTemplateParamSet(params.toArray(new String[]{""}));

            //发送短信
            SendSmsResponse response = smsClient.SendSms(request);
            log.info(SendSmsResponse.toJsonString(response));

            return true;
        } catch (Exception e) {
            return false;
        }

    }

}
